package com.lihan.GetSeatInLib.Controllers;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lihan.GetSeatInLib.Domain.Order;
import com.lihan.GetSeatInLib.Domain.SeatModel;
import com.lihan.GetSeatInLib.Domain.StandReturnMessage;
import com.lihan.GetSeatInLib.Domain.User;
import com.lihan.GetSeatInLib.Domain.Enums.CredtRank;
import com.lihan.GetSeatInLib.Domain.Enums.OrderStatus;
import com.lihan.GetSeatInLib.Domain.Enums.SeatStaus;
import com.lihan.GetSeatInLib.Service.OrderService;
import com.lihan.GetSeatInLib.Service.SeatService;
import com.lihan.GetSeatInLib.Service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "预定座位API接口")
@RestController
@RequestMapping("/Order")
public class OrderController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private SeatService seatService;
	
	@GetMapping("/CreateOrder")
	@ApiOperation("创建新的订单，（进行必要的检查）")
	public StandReturnMessage<Boolean> CreateOneOrder(String UserName,
													  Long SeatID,
													  Long Start,
													  Long End){
		
		
		
		User order_man = userService.getUserInfoByUserName(UserName);
		if(order_man.getRank()==CredtRank.Bad) {
			return new StandReturnMessage<Boolean>(400,false,0,"用户信用不足");
		}
		
		SeatModel seat = seatService.getSeatById(SeatID);
		if(seat.getStatus()==SeatStaus.LOCKED||
		   seat.getStatus()==SeatStaus.LOCKED||
		   seat.getStatus()==SeatStaus.UNABLE) {
			return new StandReturnMessage<Boolean>(400,false,0,"桌位状态不可用");
		}
		
		if(End.compareTo(Start)<0) {
			return new StandReturnMessage<Boolean>(400,false,0,"时间设置错误");
		}
		
		//创建实例，赋值
		Order NewOrder = new Order();
		NewOrder.setOwner(order_man);
		NewOrder.setSeat(seat);
		NewOrder.setCreatTime(new Date());
		NewOrder.setStatus(OrderStatus.Waited); //设置为初始状态
		NewOrder.setStartTime(new Date(Start));
		NewOrder.setEndTime(new Date(End));
		
		if(orderService.CreateOrder(NewOrder))
			return new StandReturnMessage<Boolean>(200,true,1);
		else
			return new StandReturnMessage<Boolean>(400,false,0);
	}

	//只有订单状态为初始的Waited状态时才可以使用
	@GetMapping("/ActiveOrder")
	@ApiOperation("激活订单，使之开始生效")
	public StandReturnMessage<Boolean> ActiveAOrder(Long OrderID){
		Order order = orderService.QueryOrderById(OrderID);
		if(order.getStatus()==OrderStatus.Waited) {
			//将订单修改状态修改为正在使用
			order.setStatus(OrderStatus.InUsed);
			//将订单关联的桌位的状态改为Used使用中
			boolean isSuccess = seatService.SeatInUsed(order.getSeat().getID());
			
			if(isSuccess)
				return new StandReturnMessage<Boolean>(200,true,1);
			else
				return new StandReturnMessage<Boolean>(400,false,1,"设置错误");
		}
		else
			return new StandReturnMessage<Boolean>(400,false,0,"订单状态不正确");
		
	}

	//只有订单状态为初始的Waited状态时才可以使用
	@GetMapping("/CancelOrder")
	@ApiOperation("取消订单，")
	public StandReturnMessage<Boolean> CancelOrder(Long OrderID){
		Order order = orderService.QueryOrderById(OrderID);
		if(order.getStatus()==OrderStatus.Waited) {
			order.setStatus(OrderStatus.Canceled);
			//将取消订单的桌子的状态再修改为FREE
			seatService.UnSeatInUsed(order.getSeat().getID());
			return new StandReturnMessage<Boolean>(200,true,1);
		}
		else
			return new StandReturnMessage<Boolean>(400,false,0,"订单状态不正确");
	}
	
	//在订单的日期的END_TIME前离开，也就是提前离开
//	public StandReturnMessage<Boolean> StopDuringTime(Long OrderID){
//		
//		Order order = orderService.QueryOrderById(OrderID);
//		
//		orderService.StopOrderInAdvance(order);
//		
//		boolean isSuccess = seatService.UnSeatInUsed(OrderID);
//		
//		if(isSuccess)
//			return new StandReturnMessage<Boolean>(200,true,1);
//		else
//			return new StandReturnMessage<Boolean>(400,false,0,"设置失败");
//		
//	}
	
}
